<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="@/assets/image/logo.jpg" alt="">
        <span>vueShop管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggleBtn" @click="toggleCollapse">
          <i class="iconfont icon-Shape"></i>
        </div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened='true' :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="submenu.id+''" v-for="submenu in menuList" :key="submenu.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="menuIco[submenu.id]"></i>
              <!-- 文本 -->
              <span>{{submenu.authName}}</span>
            </template>
            <el-menu-item :index="'/'+menuItem.path" v-for="menuItem in submenu.children" :key="menuItem.id" @click="saveNavState('/'+menuItem.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="menuSubIco[menuItem.id]"></i>
                <!-- 文本 -->
                <span>{{menuItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { Authority } from '@/api/apiConfig'
import { menuIco, menuSubIco } from '@/plugins/menuIco'
export default {
  name: 'home',
  data () {
    return {
      menuList: [],
      menuIco,
      menuSubIco,
      isCollapse: false,
      activePath: '',
      defaultOpeneds: []
    }
  },
  mounted () {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('defaultActive')
    // 更简单的方式直接使用:default-active="$route.path"
  },
  watch: {
    '$route' (to, from) {
      if (to.name === 'welcome') {
        this.activePath = ''
      }
    }
  },
  methods: {
    loginOut () {
      this.$jsCookie.remove('token')
      this.$router.push({
        path: '/login'
      })
    },
    // 获取左侧菜单
    getMenuList () {
      Authority.getMenuList().then(res => {
        if (res.meta.status !== 200) return this.$message.error(res.meta.message)
        this.menuList = res.data
      })
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (path) {
      sessionStorage.setItem('defaultActive', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  padding-left: 5px;
  img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    vertical-align: middle;
  }
  span {
    color: #fff;
    padding-left: 10px;
    font-size: 16px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 8px;
}
.toggleBtn {
  background: #4a5064;
  font-size: 18px;
  line-height: 30px;
  text-align: center;
  color: #fff;
}
</style>
